<template>
<div v-if="show" @click="maskClick" class="wrapper">

 <div style="width: 80%;background: white;padding: 15px;" @click.stop.prevent>
   <div style="padding: 10px 0;font-size: 18px;font-weight: bold;color: #656D78;">
     退货商品汇总
   </div>
   <div style="display: flex;">
     <div style="width: 380px;">
       <div>扫描销售单</div>
       <div>
         <t-grid :fields="cols1" :data-list="list1" :grid-height="500" :showFooter="false" />
       </div>
     </div>
     <div style="flex:1;">
       <div>关联退货汇总</div>
       <div>
         <t-grid :fields="cols2" :data-list="list2" :grid-height="500" :showFooter="false" />
       </div>
     </div>
     <div>
       <!--    <lbtn type="primary" size="large" @click="$emit('search')">查询</lbtn>-->
       <div><lbtn type="primary" size="large" @click="print">打印小票</lbtn></div>
       <div style="margin-top: 200px;"><lbtn   size="large" @click="maskClick">关闭</lbtn></div>
     </div>
   </div>
 </div>
  <doprint />
</div>
  <div id="printHtml" v-html="showPrint" style="position: absolute;top: -9999px;left: -9999px;" ></div>
</template>
<script>
import tool from "../../../utils/tool";
import Doprint from "../doprint.vue";
import xe from "xe-utils";

export default {
  name: "gpback",
  components: {Doprint},
  data () {
    return {
      show: false,
      cols1: [
        {type: 'index', width: 50},
        {field:'buName', title: '客户', width: 200,align: 'left'},
        {field:'billNo', title: '票号', width: 100},
      ],
      cols2: [
        {type: 'index', width: 50},
        {field:'invName', title: '商品', width: 300,align: 'left'},
        {field:'assQty', title: '数量', width: 120,align: 'right'},
      ],
      relList: [],
      sendPrintName: '',
      salePrintName: '',
      showPrint: ''
    }
  },
  emits: ['close','search'],
  computed: {
    list1 () {
      if(this.relList.length > 0) {
        let lis = []
        let nogp = tool.xe.groupBy(this.relList, 'billNo')
        for(let i in nogp) {
          lis.push(nogp[i][0])
        }
        return lis
      }
      return []
    },
    list2 () {
      if(this.relList.length > 0) {
        let lis = []
        let nogp = tool.xe.groupBy(this.relList, 'invId')
        for(let i in nogp) {
          let goods = []
          nogp[i].map(row => {
            goods.push({
              invId: row.invId,
              invName: row.invName,
              qty: tool.xe.multiply(row.qty, row.calNum),
              unit1: row.unit1,
              unit2: row.unit2,
              spec: row.spec
            })
          })
          let qty = tool.xe.sum(goods,'qty')
          let ass = tool.toAssQty({qty,unit1: goods[0].unit1,unit2:goods[0].unit2, spec: goods[0].spec})
          lis.push({
            invName: goods[0].invName,
            assQty: ass,
          })
        }
        return lis
      }
      return []
    },
  },
  created() {
    this.getPrintName()
  },
  methods: {
    maskClick () {
      this.show = false
      this.$emit('close')
    },
    open () {
      this.relList = []
      this.show = true
    },
    addData (data) {
      let ids = []
      this.relList.map(row => {
        ids.push(row.id)
      })
      data.map(row => {
        if(ids.indexOf(row.id) === -1) {
          this.relList.push(row)
        }
      })
    },
    print () {
      this.sendPrint()
    },
    // 取打印机名称
    getPrintName () {
      tool.post('deliver/getPrintName', {}).then(ret => {
        if (ret.stat) {
          if (ret.data.length === 0) return tool.err('没有发现打印机')
          let sendPrints = []
          let salePrints = []
          for (let i = 0; i < ret.data.length; i++) {
            if (ret.data[i].sendPrintName) sendPrints.push(ret.data[i].sendPrintName.trim())
            if (ret.data[i].salePrintName) salePrints.push(ret.data[i].salePrintName.trim())
          }
          const LODOP = (typeof getCLodop === 'function') ? getCLodop() : ''
          if (LODOP) {
            let c = LODOP.GET_PRINTER_COUNT()
            if (!c) return tool.err('未发现打印机')
            for (let i = 0; i < c; i++) {
              let name = LODOP.GET_PRINTER_NAME(i)
              if (!name) continue
              if (sendPrints.indexOf(name.trim()) !== -1) {
                this.sendPrintName = name
              }
              if (salePrints.indexOf(name.trim()) !== -1) {
                this.salePrintName = name
              }
            }
          }
        }
      })
    },
    sendPrint() {
      let gpGoods = this.list2
      if(gpGoods.length === 0) {
        return tool.err('没有汇总数据')
      }
      LODOP.PRINT_INIT("汇总小票打印");
      LODOP.SET_LICENSES("","C748D766DD08B5AE84E6BC2F3A546E6C7C9","","");
      let tbhtml = '<table style="border-collapse:collapse;width:270px;"><tbody>'
      //
      tbhtml += "<tr><td colspan='2' style='padding: 5px 0;text-align: left;font-weight: bolder;font-size: 14px;width:270px;'>商品汇总</td></tr>"
      for (let i = 0; i < gpGoods.length; i++) {
        tbhtml += '<tr><td style="padding: 2px 0;width: 200px;text-align: left;">'+gpGoods[i].invName+'</td><td style="width: 70px;text-align: right;">'+gpGoods[i].assQty+'</td></tr>'
      }
      tbhtml += "<tr><td style='border-bottom: 1px solid black;'></td><td style='border-bottom: 1px solid black'></td></tr>"
      tbhtml += "<tr><td colspan='2' style='border-top: 1px solid black;height: 110px;'></td> </tr>"
      tbhtml += '</tbody></table>'
      this.showPrint = tbhtml
      let dom = document.getElementById("printHtml");
      dom.innerHTML = tbhtml


      setTimeout(() => {
        dom = document.getElementById("printHtml");
        let rects = dom.getClientRects()
        let barTop = tool.minus(rects[0].height, 120)
        if (barTop < 300) barTop += 20
        // LODOP.ADD_PRINT_BARCODE(barTop+'px', '90px', '100px', '100px', 'QRCode', '180001-' + item.id);
        this.top = barTop
        LODOP.SET_PRINT_PAGESIZE(3, '80mm',45,"") //这里3表示纵向打印且纸高“按内容的高度”；1385表示纸宽138.5mm；45表示页底空白4.5mm
        LODOP.ADD_PRINT_TABLE(40, 0, 800, rects[0].height, tbhtml)

        if (LODOP.SET_PRINTER_INDEX(this.sendPrintName)) {
          // LODOP.PREVIEW();
          LODOP.PRINT();
          this.chosePrint = false
          // LODOP.PREVIEW();
          //   if (LODOP.CVERSION) {
          //     CLODOP.On_Return = (TaskID,Value)=> {
          //       if (Value !== 0 || Value !== false) {
          //         tool.post('print/billPrintSuc', {cargoId: item.id}).then(ret => {})
          //       }
          //     }
          //   }

        } else {
          tool.err('读取小票打印机失败')
        }
      }, 50)
    },
  }
}
</script>
<style scoped lang="scss">
.wrapper {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0,0,0,0.3);
  padding: 15px;
  border-radius: 4px;
  z-index: 999;
}
</style>